!function (window, $) {

    var currentPage = 1,
        showPage = 3,
        total;
    var listurl;
    var ip='192.168.0.149';
    var init = function () {
        initEvent();
        getData();
        // getPage
    };

    var initEvent = function () {
        $('.all').on('click', moRenData);
        $('.techer_zheng').on('click', jszClk);
        $('.alls').on('click', meici1Clk);
        $('.child_zheng').on('click', yeyClk);
        $('.monerier').on('click', jjsClk);
        $('.teach_classify').on('click', jxClk);
        $('.doctor').on('click', ysClk);
        $('.tops').click(gtOnclick);
        $('.div1').on('click', '.jump_btn', jumpBtnClk);
        $('.div1').on('click', 'li:not(.disabled)', onJumpLiClk);
        $('.zhibo').on('click', zbClk);
        $('.lubo').on('click', lbClk);
        $('.onetoone').on('click', ydyClk);
        $('.else').on('click', qtClk);
        $('.zuixin').on('click', zxClk);
        $('.zuire').on('click', zrClk);
        $('.zonghe').on('click', zhClk);
        $('.fdj_img').on('click', searchClk);
        $('.kecheng_wp').on('click','li',goXiangQing);
    };

    var goXiangQing=function(){
        location.href='./eduXiang.html?id='+$(this).attr('data-p');
    };

    var clActive = function (o) {
        o.addClass('active');
        o.prevAll().removeClass('active');
        o.nextAll().removeClass('active');
    };

    var searchClk = function () {
        var $iptVle = $('.sea_ipt').val();
        // var dizhi = 'http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12' + '&order=id&filter=title,cs,' + $iptVle;
        var dizhi ='http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';
        getData(dizhi);
    };

    var zhClk = function () {
        // listurl = 'http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12' + '&order=nowmoney,desc';
        listurl = 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';
        clActive($(this));
        getData(listurl);
    };

    var zxClk = function () {
        // listurl = 'http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12' + '&order=id,desc';
        listurl = 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';

        clActive($(this));
        getData(listurl);
    };

    var zrClk = function () {
        // listurl = 'http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12' + '&order=people,desc';
        listurl = 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';

        clActive($(this));
        getData(listurl);
    };

    var qtClk = function () {
        // listurl = 'http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12' + '&filter=moshi,cs,其他';
        listurl = 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';
        clActive($(this));
        getData(listurl);
    };

    var ydyClk = function () {
        listurl = 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';
        // listurl = 'http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12' + '&filter=moshi,cs,一对一教学';
        clActive($(this));
        getData(listurl);
    };

    var lbClk = function () {
        listurl = 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';
        // listurl = 'http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12' + '&filter=moshi,cs,录播';
        clActive($(this));
        getData(listurl);
    };

    var zbClk = function () {
        listurl = 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';
        // listurl = 'http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12' + '&filter=moshi,cs,直播';
        clActive($(this));
        getData(listurl);
    };

    var meici1Clk = function () {
        listurl = 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';
        // getData('http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12');
        clActive($(this));
    };

    var ysClk = function () {
        var abc = '药师';
        listurl = 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';
        // listurl = 'http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12' + '&filter=type,cs,' + abc;
        getData(listurl);
        clActive($(this))
    };

    var jxClk = function () {
        listurl = 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';
        // listurl = 'http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12' + '&filter=type,cs,教学';
        getData(listurl);
        clActive($(this));
    };

    var jjsClk = function () {
        listurl = 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';
        // listurl = 'http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12' + '&filter=type,cs,经济师';
        getData(listurl);
        clActive($(this));
    };

    var jszClk = function () {
        console.log(currentPage);
        listurl = 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';
        // listurl = 'http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12' + '&filter=type,cs,教师';
        getData(listurl);
        clActive($(this));
    };

    var yeyClk = function () {
        listurl = 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';
        // listurl = 'http://localhost/api.php/yuzhiyuan/kczx?transform=1&order=id&page=' + currentPage + ',12' + '&filter=type,cs,园长';
        getData(listurl);
        clActive($(this));
    };

    var jumpBtnClk = function () {
        var $jumpIpt = $('.jump_ipt');
        currentPage = $jumpIpt.val() * 1;
        getData();
    };

    var onJumpLiClk = function () {
        currentPage = $(this).attr('data-p') * 1;
        getData();
    };

    var getData = function (url) {
        console.log(url);
        console.log(currentPage);
        var urls = url ? url : 'http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12';
        var ajax;
        if (window.XMLHttpRequest) {
            ajax = new XMLHttpRequest();
        } else {
            ajax = new ActiveXObject(Microsoft.XMLHttp);
        }
        ajax.open('get', urls);
        ajax.send();
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4 && ajax.status == 200) {
                // console.log(JSON.parse(ajax.responseText));
                $('.pages').children().html(JSON.parse(ajax.responseText).data.count);
                total=Math.ceil((JSON.parse(ajax.responseText).data.count)/12);
                $('.div1').fenye({
                    allPage: total,
                    curPage: currentPage,
                    showPage: showPage
                });
                render(JSON.parse(ajax.responseText).data.list);
            }
        }
    };

    var moRenData = function () {
        getData('http://'+ip+'/tp5/public/index.php/index/classall?pageCurrent='+currentPage+'&pageSize=12');
        clActive($(this));
    };


    var render = function (o) {
        // console.log(o);
        var listArr = [];
        var $kechengwp = $('.kecheng_wp');
        // console.log(o);
        o.length != 0 ? $.each(o, function (i, a) {
            listArr.push('<li class="kecheng_item" data-p=',a.id,'>',
                '<img src="',a.img,'" alt="" class="kecheng_img">',
                '<div class="kecheng_font">',
                '<p class="kecheng_title">', a.title, '</p>',
                '<p class="kecheng_content">', a.content, '</p>',
                '</div>',
                '<div class="kecheng_bottom">',
                '<div class="kecheng_bottom_wp">',
                '<div class="clear">',
                '<div class=" fl">',
                '<p class="kecheng_money">￥' + a.price,
                '<span class="agomoney">￥', a.price_old, '</span>',
                '</p>',
                '</div>',
                '<div class="fr">',
                '<img src="http://ilty.gitee.io/img/YuZhiYuan/shoye/daling.png" alt="" class="bottom_img">',
                '<span class="bottom_user">', a.people, '</span>',
                '</div>',
                '</div>',
                '<div>',
                '<p class="moshi">授课模式：',
                '<span>', moshiCl(a.type), '</span>',
                '</p>',
                '</div>',
                '</div>',
                '</div>',
                '</li>')
        }) : listArr.push('<p class="zanwu">暂时还没有商品信息~</p>')
        $kechengwp.html(listArr.join(''));
        chuLiFon();
    };


    var moshiCl=function(o){
        if(o==1){
            return '直播'
        }else if(o==2){
            return '录播'
        }
    };

    var chuLiFon = function () {
        // var kechengMoney=document.querySelectorAll('.kecheng_money');
        var $agomoney = $('.agomoney');
        $('.agomoney:empty').parent().css({
            color: '#25c2b7'
        });
        // $.each($kechengMoney,function(i,o){
        //     console.log(o.children());
        // })
    };

    window.onscroll = function () {
        var $gt = $('.tops');
        var osTop = document.documentElement.scrollTop;
        if (osTop > 500) {
            $gt.css({
                'display': 'block'
            })
        } else {
            $gt.css({
                "display": "none"
            })
        }
    };

    var gtOnclick = function () {
        timer = setInterval(function () {
            var osTop = document.documentElement.scrollTop;
            var speed = Math.floor(-osTop / 6);
            document.documentElement.scrollTop = document.body.scrollTop = osTop + speed;
            if (osTop == 0) {
                clearInterval(timer);
            }
        }, 10)
    };

    init();
}(window, jQuery, undefined);